package newProject;

import javax.swing.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

public class MultiplyThread implements Runnable{
    ExecutorService threadPool= Executors.newCachedThreadPool();//创建线程池
    static ExecutorService es=new MultiplyThread().threadPool;
    @Override
    public void run() {


        GetWebResource.main();
    }
    public static void stopThread(){
        XJiangJFrame.startFlag=false;//线程结束 为false
        es.shutdownNow();

    }
    public static void startThread(String num){
            XJiangJFrame.startFlag=true;//true为开启
            try{
                if(!Pattern.compile("[0-9]").matcher(num).matches())//输入的线程数是否是数字
                {throw new NumberFormatException();}
                int number=Integer.parseInt(num);//字符串转数字
                while(number!=0) {
                    number--;
                    if (es.isShutdown()) es = new MultiplyThread().threadPool;//怕点击关闭按钮 再开始报错，判断线程池是否还在
                    es.submit(new MultiplyThread());//提交线程到线程池 自动执行run方法的内容
                }
            }
            catch (NumberFormatException e){

                JOptionPane.showMessageDialog(new JPanel(),"请输入1到10的线程数","温馨提示",JOptionPane.INFORMATION_MESSAGE);
            }
    }
 }




